package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.mapper.BrandMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    public void deleteById(Long id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Brand brand) {
        if (brand.getId() != null) {
            brandMapper.updateByPrimaryKey(brand);
        } else {
            brandMapper.insert(brand);
        }
    }

    public Brand getById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    public List<Brand> selectAll() {
        return brandMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = brandMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Brand> data = brandMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void deleteall(Long[] ids) {
        if (ids!=null){
            for (Long id : ids) {
                brandMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
